﻿<script src="/Plugins/print/jquery.PrintArea.min.js"></script>
<div class="layui-card" id="lg-ExamInfo" v-if="exam!=null">
    <div class="layui-card-header">
        <i class="fa fa-home fa-lg fa-fw"></i><a class="lg-frame-link" href="javascript:;" url="/Lgm/Apply/Exam/List"> 考试计划</a> > 设置考试
        <div class="tool layui-inline" style="float: right">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-warm" v-on:click="GetExam" title="刷新页面"><i class="fa fa-refresh fa-fw fa-xs" aria-hidden="true"></i>刷新</button>
            <button type="button" class="layui-btn layui-btn-sm" v-on:click="Back" title="返回"><i class="fa fa-reply fa-fw fa-xs" aria-hidden="true"></i>返回</button>
        </div>
    </div>
    <!--状态栏-->
    <div class="layui-card-body lg-vue">
        <div class="layui-form-item">
            <div class="layui-form-item">
                <blockquote class="layui-elem-quote layui-text">
                    <table class="lg-table lg-table-auto lg-table-black">
                        <thead>
                            <tr>
                                <th>考试名称</th>
                                <th>计划年级</th>
                                <th>准考证号</th>
                                <th>考试按排</th>
                                <th>成绩导入</th>
                                <th style="width:110px;text-align:center">打印状态</th>
                                <th style="width:110px;text-align:center">成绩查询</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>{{exam.Title}}</td>
                                <td>{{exam.GradeFullName}}</td>
                                <td style="text-align:center"><i class="fa fa-check text-success" title="已生成" v-if="exam.SetNumState==2"></i><i class="fa fa-close text-wrong" title="未生成" v-else></i></td>
                                <td style="text-align:center"><i class="fa fa-check text-success" title="已按排" v-if="exam.SetExamState==2"></i><i class="fa fa-close text-wrong" title="未按排" v-else></i></td>
                                <td style="text-align:center"><i class="fa fa-check text-success" title="已导入" v-if="exam.SetScoreState==2"></i><i class="fa fa-close text-wrong" title="未导入" v-else></i></td>
                                <td style="text-align:center">
                                    <div class="layui-table-cell laytable-cell-4-0-6">
                                        <div :class="['layui-unselect layui-form-switch',{'layui-form-onswitch':exam.IsPrint==1}]" style="margin-top:0px" v-on:click="Switch('IsPrint')" lay-skin="_switch">
                                            <em>{{exam.IsPrint==1?'ON':'OFF'}}</em><i></i>
                                        </div>
                                    </div>
                                </td>
                                <td style="text-align:center">
                                    <div class="layui-table-cell laytable-cell-4-0-6">
                                        <div :class="['layui-unselect layui-form-switch',{'layui-form-onswitch':exam.IsQuery==1}]" style="margin-top:0px" v-on:click="Switch('IsQuery')" lay-skin="_switch">
                                            <em>{{exam.IsQuery==1?'ON':'OFF'}}</em><i></i>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </blockquote>
            </div>
            <div class="layui-tab">
                <ul class="layui-tab-title">
                    <li class="layui-this">考生名单</li>
                </ul>
                <div class="layui-tab-content">
                    <!--考生名单-->
                    <div class="layui-tab-item layui-show">
                        <!--查询条件-->
                        <div class="layui-form-item">
                            <div class="lg-search lg-layout-row lg-layout-row-auto">
                                <div class="lg-search-item" style="width:120px">
                                    <lg-form-singletext :data="formData[0]" v-model:value="table.where.Name"></lg-form-singletext>
                                </div>
                                <div class="lg-search-item" style="width:180px">
                                    <lg-form-idnumber :data="formData[1]" v-model:value="table.where.IdNumber"></lg-form-idnumber>
                                </div>
                                <div class="lg-search-item" style="width:150px">
                                    <lg-form-select :data="formData[2]" v-model:value="table.where.PrintState" val-type="value"></lg-form-select>
                                </div>
                                <div class="lg-search-item" style="width:150px">
                                    <lg-form-select :data="formData[3]" v-model:value="table.where.IsPass" val-type="value"></lg-form-select>
                                </div>
                                <div class="lg-search-item layui-input-inline" style="width:auto">
                                    <button class="layui-btn" type="button" v-on:click="OnSearch">查询</button>
                                </div>
                            </div>
                        </div>
                        <div class="lg-form-box">
                            <!--头部工具条-->
                            <div class="lg-table-tool" id="toolbar">
                                <div class="layui-btn-container">
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" v-on:click="ClearStudentEvent"><i class="fa fa-trash-o fa-fw" aria-hidden="true"></i>清除考生</button>
                                    <button type='button' class="layui-btn layui-btn-sm" v-on:click="ImportStudentEvent"><i class="fa fa-sign-in fa-fw" aria-hidden="true"></i>导入考生</button>
                                    <button type='button' class="layui-btn layui-btn-sm" v-on:click="SyncStudent()"><i class="fa fa-sign-in fa-fw" aria-hidden="true"></i>迁入考生</button>
                                    <button type='button' class="layui-btn layui-btn-sm" v-on:click="SetTicketNumber()"><i class="fa fa-sort-numeric-asc fa-fw"></i>生成准考证</button>
                                    <button type='button' class="layui-btn layui-btn-sm" v-on:click="ExportStudentsEvent"><i class="fa fa-file-excel-o fa-fw" aria-hidden="true"></i>导出考生</button>
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-warm" v-on:click="ImportScoreEvent"><i class="fa fa-file-excel-o fa-fw" aria-hidden="true"></i> 导入成绩</button>
                                    <div class="lg-inline-block"><button type='button' class="layui-btn layui-btn-sm layui-btn-normal" v-on:click="SendSMSForPrint()"><i class="fa fa-envelope-o fa-fw"></i>打印通知</button></div>
                                    <div class="lg-inline-block"><button type='button' class="layui-btn layui-btn-sm layui-btn-normal" v-on:click="SendSMSForQuery()"><i class="fa fa-envelope-o fa-fw"></i>查询通知</button></div>
                                    <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" v-on:click="ExamSettingEvent"><i class="fa fa-cog fa-fw" aria-hidden="true"></i>考试安排</button>
                                    <button type="button" class="layui-btn layui-btn-sm" v-on:click="GetExamStudent"><i class="fa fa-refresh fa-fw" aria-hidden="true"></i> 刷新</button>
                                </div>
                            </div>
                            <!--table-->
                            <div class="lg-table-body">
                                <table class="lg-table">
                                    <thead>
                                        <tr>
                                            <th class="lg-table-check">
                                                <div :class="['layui-unselect layui-form-checkbox',{'layui-form-checked':table.data.length>0&&table.selected.length==table.data.length}]" lay-skin="primary"><i class="layui-icon layui-icon-ok"></i></div>
                                            </th>
                                            <th v-for="item in table.column">{{item.title}}</th>
                                            <th style="width:160px">操作</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr v-for="row in table.data">
                                            <td class="lg-table-check">
                                                <div :class="['layui-unselect layui-form-checkbox']" lay-skin="primary"><i class="layui-icon layui-icon-ok"></i></div>
                                            </td>
                                            <td v-for="column in table.column" v-html="FormatColumnValue(column.name,row)" v-on:click="ColumnEvent($event,column.name,row)"></td>
                                            <td>
                                                <a class="layui-btn layui-btn-xs layui-btn-normal" title="查看或设置考生参数" v-on:click="StudentExamInfoEvent(row)"><span class="fa fa-cog fa-fw "></span></a>
                                                <a class="layui-btn layui-btn-xs layui-btn-warm" title="发送打印通知" v-on:click="SendSinglePrintSMSEvent(row)"><span class="fa fa-envelope-o fa-fw "></span></a>
                                                <a class="layui-btn layui-btn-xs layui-btn-danger" title="删除学生" v-on:click="DeleteExamStudentEvent(row)"><span class="fa fa-trash-o fa-fw"></span></a>

                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <div class="LgTable-NoData" v-if="table.data.length==0||table.loading">
                                    <p v-if="!table.loading">没有数据 ~</p>
                                    <p v-if="table.loading"><img src="/Plugins/layer/theme/default/loading-2.gif" style="width:20px" /> 数据加载中...</p>
                                </div>
                            </div>
                            <!--分页-->
                            <div id="pager" style="margin-top:-10px;"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--导入学生-->
    <div class="fieldWrap" id="ImportStudent" style="position:relative;display:none">
        <p>提示：</p>
        <div class="field-help">
            <blockquote>
                1、学生数据可从报名记录中自行导出<br />
                2、重复项(身份证)则用新数据替换旧数据<br />
                3、导入列：姓名、性别、证件号、手机号、标识、流水号<br />
                4、请确保列名一致且不含空字符！
            </blockquote>
        </div>
        <div class="field-foot">
            <input type="file" id="StudentTableSelector" class="hide" accept="xls,xlsx" v-on:change="ImportStudent" ref='StudentTableSelector'>
            <div class="fileUpBox" v-on:click="this.$refs.StudentTableSelector.click()"><span class="fa fa-cloud-upload btn-file">上传</span></div>
        </div>
    </div>
    <!--打印弹层-->
    <div id="PrintWrap" style="display:none">
        <div style="text-align:center;margin:15px"><button class="layui-btn layui-btn layui-btn-normal" type="button" onclick="$('#printArea').printArea()"><i class="fa fa-print fa-fw" aria-hidden="true"></i>打印</button></div>
        <div id="printArea" v-html="printTemplate">
        </div>
    </div>
    <!--导入成绩-->
    <div class="fieldWrap" id="ImportScoreWarp" style="position:relative;display:none">
        <p>提示：</p>
        <div class="field-help">
            <p>
                1、如非首次导入成绩，则重复项会使用最新数据替换原数据,下载成绩表<br />
                2、请不要改变成绩模版的列数据<br />
                3、成绩支持分数和等第<br />
                4、考试结果请录入数字状态码：1:未通过 2:通过
            </p>
            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" v-on:click="LoadStucentScoreTemplate">下载成绩模板</button>
        </div>
        <div class="field-foot">
            <input type="file" id="ScoreSelector" class="hide" accept="xls,xlsx" v-on:change="ImportScore" ref='ScoreSelector'>
            <div class="fileUpBox" v-on:click="this.$refs.ScoreSelector.click()"><span class="fa fa-cloud-upload btn-file">上传</span></div>
        </div>
    </div>
    <!--考试按排-->
    <div id="examSettingWrap">
        <div class="step step-1" v-if="examSetor.actTags.indexOf('step-1')>-1">
            <div class="bigTitle">当前考试已按排完毕</div>
            <div>
                <button type="button" class="layui-btn layui-btn layui-btn-normal" v-on:click="ViewRuleEvent">查看规则</button>
                <button type="button" class="layui-btn layui-btn layui-btn-danger" v-on:click="ExamResetEvent">重新按排</button>
            </div>
        </div>
        <div class="step step-2" v-if="examSetor.actTags.indexOf('step-2')>-1">
            <div class="bigTitle">请选择您想使用的考试按排方式！</div>
            <div class="btWrap">
                <button type="button" class="layui-btn layui-btn layui-btn-normal" v-on:click="ExamQuickSetEvent">快速按排</button>
                <button type="button" class="layui-btn layui-btn layui-btn-danger" v-on:click="InitExamWrap('step-3')">手动按排</button>
            </div>
            <div class="exam-set-tips">
                <div class="title"><i class="fa fa-info-circle fa-fw" aria-hidden="true"></i>提示</div>
                <p>快速按排：<br />快速按排将使用默认规则批量生成所有科目的考场数据;各科目单独设定考试时间，考场生成使用统一规则,按考生人数/考场容纳人数，自动生成二位数考场号，考场全称使用"考场名称+考场号"规则，座位号则按考场容纳人数从1开始生成。</p>
                <p>手动按排：<br />手动按排则完全由用户撑控规则，按科目自由设置考试时间、考场等数据，支持多时段和自定义考场名称、人数等</p>
            </div>
        </div>
        <div class="examSetBox step-3" v-if="examSetor.actTags.indexOf('step-3')>-1">
            <div class="examSetLeft">
                <div :class="['course-list',{'act':item.km==examSetor.currProject.km}]" v-for="item in examSetor.rule" v-on:click="ExamProjectEvent(item)">{{item.km}}</div>
            </div>
            <div class="examSetRight">
                <div class="exam-rule-right" v-if="examSetor.currProject!=null" style="padding:10px 10px 110px 10px;height:100%;box-sizing:border-box">
                    <div class="layui-card-header">
                        <i class="fa fa-user fa-fw"></i>考试人数：{{exam.StudentCount}}
                        <div class="tool layui-inline" style="float: right">
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-warm" v-on:click="AddTimeEvent" title="">
                                <i class="fa fa-plus fa-fw fa-xs" aria-hidden="true"></i>添加时段
                            </button>
                        </div>
                    </div>
                    <div class="exam-sd" v-if="examSetor.currProject.sd.length>0" style="height:100%;overflow:auto">
                        <div class="layui-card" v-for="item in examSetor.currProject.sd">
                            <div class="layui-card-header">{{item.time}}（{{item.number}}人）</div>
                            <div class="layui-card-body">
                                <div class="examRoom">
                                    <span v-for="room in item.rooms">{{room.name}} ({{room.seat}}人)</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div :class="['examTimeWrap',{'act':examSetor.timeWrapShow}]">
                    <div class="layui-card-header">
                        <i class="fa fa-user fa-fw"></i>考试时段
                        <div class="tool layui-inline" style="float: right">
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" v-on:click="AddExamTime" title="确认提交时段数据"><i class="fa fa-check fa-fw fa-xs" aria-hidden="true"></i>确认</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-warm" v-on:click="" title=""><i class="fa fa-close fa-fw fa-xs" aria-hidden="true"></i>取消</button>
                        </div>
                    </div>
                    <lg-form-singletext :data="{Title:'时段'}" v-model:value="examSetor.sd.time" v-on:done="OnFormDone"></lg-form-singletext>
                    <lg-form-number :data="{Title:'人数'}" v-model:value="examSetor.sd.number" v-on:done="OnFormDone"></lg-form-number>
                    <div class="layui-card-header">
                        <span class="cn">已有考场</span>
                        <div class="tool layui-inline" style="float: right">
                            <button type="button" class="layui-btn layui-btn-sm" v-on:click="examSetor.roomWrapShow=!examSetor.roomWrapShow">
                                添加考场
                                <i :class="['fa fa-fw',{'fa-angle-down':!examSetor.roomWrapShow},{'fa-angle-up':examSetor.roomWrapShow}]"></i>
                            </button>
                        </div>
                        <div class="examRoomSlide" v-show="examSetor.roomWrapShow">
                            <div class="lg-search lg-layout-row lg-layout-row-auto">
                                <div class="lg-search-item"><lg-form-singletext :data="{Title:'考场名称'}" v-model:value="examSetor.room.name"></lg-form-singletext></div>
                                <div class="lg-search-item"><lg-form-number :data="{Title:'容纳人数'}" v-model:value="examSetor.room.seat"></lg-form-number></div>
                            </div>
                            <div style="padding:15px 0;text-align:center">
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" v-on:click="AddExamRoom(1)">单个创建</button>
                                <button type="button" class="layui-btn layui-btn-sm layui-btn-info" v-on:click="AddExamRoom(2)">批量创建</button>
                            </div>
                        </div>
                    </div>
                    <div class="examRoom">
                        <span v-for="item in examSetor.sd.rooms">{{item.name}}（{{item.seat}}人）</span>
                    </div>
                </div>
            </div>
            <div class="bt-submit">
                <div style="float:left;cursor:pointer" v-on:click="InitExamWrap('step-2')"><i class="fa fa-long-arrow-left fa-fw" aria-hidden="true"></i>返回方式选择界面</div>
                <button type="button" class="layui-btn layui-btn layui-btn-normal" v-on:click="SubmitExamSetData">保存并生成考场数据</button>
            </div>
        </div>
        <div class="step step-4" v-if="examSetor.actTags.indexOf('step-4')>-1">
            <div class="layui-card-header w-100">
                <div style="float:left;cursor:pointer" v-on:click="InitExamWrap('step-2')"><i class="fa fa-long-arrow-left fa-fw" aria-hidden="true"></i>返回方式选择界面</div>
            </div>
            <div class="" style="margin:15px">
                <div class="lg-search lg-layout-row lg-layout-row-auto">
                    <fieldset class="layui-elem-field layui-field-title w-100" style="margin-top: 20px;">
                        <legend>设置时段</legend>
                    </fieldset>
                    <div class="lg-search-item" v-for="(item,index) in examSetor.quickData.sd" style="width:25%">
                        <lg-form-singletext :data={"Title":item.name} v-model:value="item.time"></lg-form-singletext>
                    </div>
                    <fieldset class="layui-elem-field layui-field-title w-100" style="margin-top: 20px;">
                        <legend>设置考场</legend>
                    </fieldset>
                    <div class="lg-search-item"><lg-form-singletext :data="{Title:'考场名'}" v-model:value="examSetor.quickData.roomTitle"></lg-form-singletext></div>
                    <div class="lg-search-item"><lg-form-number :data="{Title:'容纳人数'}" v-model:value="examSetor.quickData.seat"></lg-form-number></div>
                    <hr class="w-100" style="margin-top:25px;line-height:36px" />
                    <div class="w-100" style="margin:25px;text-align:center"><button type="button" class="layui-btn layui-btn layui-btn-normal" v-on:click="QuickSetExam">快速按排</button></div>
                </div>
            </div>
        </div>
    </div>
    <!--查看和编辑单个学生考场数据-->
    <div id="stu_exam_set_data" style="display:none;">
        <div class="layui-card-header">
            考生[{{currStudent.Name}}]-考场数据
        </div>
        <div style="padding:10px 20px">
            <table class="lg-table">
                <tr>
                    <td>准考证 </td>
                    <td><input type="text" v-model="currStudent.TicketNum" /></td>
                </tr>
            </table>
        </div>
        <div style="padding:10px 20px">
            <table class="lg-table">
                <thead>
                    <tr>
                        <th>科目</th>
                        <th>时段</th>
                        <th>考场</th>
                        <th>座位</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in currStudent.ExamRule">
                        <td><input type="text" v-model="item.km" /></td>
                        <td><input type="text" v-model="item.time" /></td>
                        <td><input type="text" v-model="item.kc" /></td>
                        <td><input type="text" v-model="item.seat" /></td>
                    </tr>
                </tbody>

            </table>
        </div>
    </div>
</div>
<script type="module">
    import { freeForm } from '/plugins/lgform/lg.form.2.1.js'
    freeForm.addEventListener();
    frame.createPage({
        mounted: function() {
            var page = this;
            var app = Vue.createApp({
                data() {
                    return {
                        examId: @ViewBag.id,
                        exam: {},
                        search: {
                            Name: "",
                            IdNumber: "",
                            IsPass: "",
                            IsPrint: ""
                        },
                        currStudent: {},
                        formData: [
                            { Title: "姓名", Name: "Name", TypeName: "SingleText" },
                            { Title: "证件号", Name: "IdNumber", TypeName: "IdNumber", },
                            { Title: "打印", Name: "PrintState", TypeName: "Select", Options: [{ Text: "不限", Value: "" }, { Text: "未打印", Value: 1 }, { Text: "已打印", Value: 2 }] },
                            { Title: "结果", Name: "IsPass", TypeName: "Select", Options: [{ Text: "不限", Value: "" }, { Text: "未通过", Value: 1 }, { Text: "通过", Value: 2 }] }
                        ],
                        printTemplate: "",
                        table: {
                            selected: [],
                            column: [],
                            data: [],
                            loading: false,
                            where: {
                                page: 1,
                                limit: 10,
                                ExamId: @ViewBag.id,
                                Name: "",
                                IdNumber: "",
                                PrintState: "",
                                IsPass: ""
                            }
                        },
                        examSetor: {
                            rule: [],
                            currProject: null,
                            currIndex: 0,
                            actTags: [],
                            timeWrapShow: false,
                            rommWrapShow: false,
                            room: { name: "", seat: 0 },
                            sd: {
                                time: "",
                                number: 0,
                                rooms: [],
                            },
                            quickData: {
                                sd: [],
                                roomTitle: "",
                                seat:0
                            }
                        }
                    }
                },
                mounted: function() {
                    this.GetExam();
                },
                methods: {
                    Back: function() {
                        frame.Back(page.url, res => {
                            res.laytable.reload();
                        })
                    },
                    //处理table列
                    ColumnHandle: function() {
                        var col = [
                            { title: "#", name: "Id" },
                            { title: "姓名", name: "Name" },
                            { title: "性别", name: "Gender" },
                            { title: "身份证", name: "IdNumber" },
                            { title: "准考证", name: "TicketNum" },
                            { title: "打印", name: "PrintState" }
                        ];
                        if (this.exam.Project && this.exam.Project.split("|").length > 0) {
                            var projectArry = this.exam.Project.split("|");
                            for (let i in projectArry) {
                                col.push({ title: projectArry[i], name: projectArry[i] })
                            }
                        }

                        col.push({ title: "结果", name: "IsPass" }, { title: "评语", name: "Comment" }, { title: "添加日期", name: "AddTime" });
                        this.table.column = col;
                    },
                    //格式化状态
                    FormatColumnValue: function(key, obj) {
                        var isScoreColumn = false;
                        if (this.exam.Project && this.exam.Project.split("|").length > 0) {
                            isScoreColumn = this.exam.Project.split("|").filter(p => p == key).length > 0;
                        }

                        if (isScoreColumn) {
                            var score = lg.convert.toJson(obj.Score, null);
                            if (score != null && score[key] != undefined) {
                                return score[key]
                            }
                        }
                        else if (key == "PrintState") {
                            switch (obj.PrintState) {
                                case 2:
                                    return "<button type='button' class='layui-btn layui-btn-primary layui-btn-xs print-exam lg-eventor'><i class='fa fa-print lg-eventor'></i ></button >" +
                                        " (<i class='fa fa-check text-success' title='已打印'></i>)";
                                default:
                                    return "<button type='button' class='layui-btn layui-btn-primary layui-btn-xs print-exam lg-eventor'><i class='fa fa-print lg-eventor'></i ></button >" +
                                        " (<i class='fa fa-close text-wrong' title='未打印'></i>)";
                                    break;
                            }
                        } else if (key == "IsPass") {
                            switch (obj.IsPass) {
                                case 1:
                                    return "未通过";
                                    break;
                                case 2:
                                    return "已通过";
                                    break;
                                default:
                                    return "";
                            }
                        }
                        else {
                            return obj[key];
                        }

                    },
                    //考生table单元格点击事件捕获
                    ColumnEvent: function(event, key, obj) {
                        if (event.target.className.indexOf("lg-eventor") == -1) {
                            return false;
                        }
                        //准考证打印事件
                        if (key == "PrintState") {
                            var promise = this.GetPrintTemplate(obj.Id);
                            promise.then(res => {
                                if (res.code == 0) {
                                    this.printTemplate = res.data;
                                    layer.open({
                                        type: 1
                                        , title: '打印准考证'
                                        , area: ['800px', '650px']
                                        , shade: 0
                                        , maxmin: true
                                        , content: $("#PrintWrap")
                                    });
                                } else {
                                    layer.msg(res.msg, { icon: 2 })
                                }
                            })
                        }
                    },
                    //插入考试科目
                    GetExamStudent: function() {
                        this.table.data = [];
                        this.table.loading = true;
                        lg.request({
                            url: '/api/sys/apply/GetExamStudentList',
                            data: this.table.where,
                            success: res => {
                                this.table.loading = false;
                                if (res.code == 0) {
                                    if (res.data != null) {
                                        this.table.data = res.data;
                                    }
                                    this.SetPager(res.count)
                                } else {
                                    layer.msg(res.msg, { icon: 2 })
                                }
                            }
                        })
                    },
                    //设置分页
                    SetPager: function(count) {
                        var that = this;
                        layui.laypage.render({
                            elem: 'pager'
                            , curr: that.table.where.page
                            , limit: that.table.where.limit
                            , count: count
                            , layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                            , jump: function(obj, first) {
                                if (!first) {
                                    that.table.where.page = obj.curr;
                                    that.table.where.limit = obj.limit;
                                    that.GetExamStudent();
                                }
                            }
                        });
                    },
                    //查询按钮点击事件
                    OnSearch: function() {
                        this.table.where.page = 1;
                        this.GetExamStudent();
                    },
                    //获取考试详情
                    GetExam: function() {
                        lg.request({
                            url: "/api/sys/apply/GetExam/" + this.examId,
                            success: res => {
                                if (res.code == 0) {
                                    this.exam = res.data[0];
                                    this.ColumnHandle();
                                    this.GetExamStudent();
                                } else {
                                    layer.msg("考试按排获取失败！", { icon: 2 })
                                }
                            }
                        })
                    },
                    //清除考生按钮事件
                    ClearStudentEvent() {
                        var that = this;
                        layer.confirm("此操作将清除该考试计划下面的所有考生，并重置考试计划的准考证生成状态、考试按排、成绩导入状态，执行结果无法恢复！是否确认执行？", { area: ['400px', 'auto'] }, function(box) {
                            layer.close(box);
                            lg.request({
                                url: "/api/sys/apply/clearexamstudents",
                                data: { ExamId: that.examId },
                                method: "post",
                                success: res => {
                                    if (res.code == 0) {
                                        that.GetExam();
                                        layer.msg("执行成功！", { icon: 1 })
                                    } else {
                                        layer.msg(res.msg, { icon: 2 })
                                    }
                                }
                            })
                        })
                    },
                    //生成准考证
                    SetTicketNumber: function() {
                        var that = this;
                        layer.confirm(" 非首次执行准考证生成会覆盖之前的数据，是否立即生成考生准考证号？", function(index) {
                            lg.request({
                                url: "/api/sys/apply/SetTicketNum",
                                data: { ExamId: that.examId },
                                method: "post",
                                success: res => {
                                    if (res.code == 0) {
                                        layer.msg(res.msg, { icon: 1 });
                                        that.exam.SetNumState = 2;
                                        that.GetExamStudent();

                                    } else {
                                        layer.msg(res.msg, { icon: 2 })
                                    }
                                }
                            })
                            layer.close(index)
                        })

                    },
                    //从计划同步学生
                    SyncStudent: function() {
                        var that = this;
                        if (this.exam.PlanId > 0) {
                            layer.confirm("本功能将同步考试绑定的招生计划中的学生，重复项将跳过，是否确认继续执行？", function(index) {
                                lg.request({
                                    url: "/api/sys/apply/AddExamStudentFromPlan",
                                    method: "post",
                                    data: { "ExamId": that.exam.Id },
                                    success: res => {
                                        if (res.code == 0) {
                                            layer.msg(res.msg, { icon: 1 });
                                            that.GetExam();
                                        } else {
                                            layer.msg(res.msg, { icon: 2 });
                                        }
                                    }
                                })
                            })
                        } else {
                            layer.msg("该考试未关联招生计划，无法使用该功能,试试导入考生吧！", { icon: 2 })
                        }
                    },
                    //导入考生按钮事件
                    ImportStudentEvent: function() {
                        layer.open({
                            type: 1,
                            content: $("#ImportStudent")
                        })
                    },
                    //Excel导入考生
                    ImportStudent: function(event) {
                        var that = this;
                        var event = this.$refs.StudentTableSelector;
                        var files = event.files;
                        //较验文件数，未选择文件则不执行任何事件
                        if (files.length == 0) {
                            return false;
                        }
                        var fd = new FormData();
                        fd.append("file", files[0]);
                        fd.append("examId", this.exam.Id);
                        lg.postFile({
                            url: "/api/sys/apply/ImportExamStudent",
                            data: fd,
                            success: res => {
                                if (res.code == 0) {
                                    layer.closeAll();
                                    layer.msg(res.msg, { icon: 1 });
                                    this.GetExam();
                                } else {
                                    layer.msg(res.msg, { icon: 2 })
                                }
                            }
                        })

                        event.value = "";

                    },
                    //根据条件导出考生数据
                    ExportStudentsEvent: function() {
                        lg.getFile({
                            url: "/api/sys/apply/ExportExamStudents",
                            data: this.table.where,
                            fileName: this.exam.Title + "-考生数据.xlsx"
                        });
                    },
                    //导出考生成绩模版
                    LoadStucentScoreTemplate: function() {
                        lg.getFile({
                            url: "/api/sys/apply/LoadScroeTemplate",
                            data: { examId: this.exam.Id },
                            fileName: this.exam.Title + "-成绩模板"
                        });
                    },
                    //导入成绩按钮事件
                    ImportScoreEvent: function(event) {
                        layer.open({
                            type: 1,
                            content: $("#ImportScoreWarp")
                        })
                    },
                    //执行文件上传和导入成绩
                    ImportScore: function(event) {
                        var event = this.$refs.ScoreSelector;
                        var files = event.files;
                        //较验文件数，未选择文件则不执行任何事件
                        if (files.length == 0) {
                            return false;
                        }
                        var fd = new FormData();
                        fd.append("file", files[0]);
                        fd.append("examId", this.exam.Id);
                        lg.postFile({
                            url: "/api/sys/apply/ImportScore",
                            data: fd,
                            success: res => {
                                if (res.code == 0) {
                                    layer.closeAll();
                                    layer.msg(res.msg, { icon: 1 });
                                    this.GetExamStudent();
                                } else {
                                    layer.msg(res.msg, { icon: 2 })
                                }
                            }
                        })

                        event.value = "";
                    },
                    //打印、查询开关,0/1值的exam所有属性通用
                    Switch: function(key) {
                        var data = { "Id": this.exam.Id };
                        data[key] = this.exam[key] == 1 ? 0 : 1;
                        lg.request({
                            url: "/api/sys/apply/EditExam",
                            method: "post",
                            data: data,
                            success: res => {
                                if (res.code == 0) {
                                    this.exam[key] = data[key];
                                    layer.msg("执行成功!", { icon: 1 })
                                } else {
                                    layer.msg(d.msg, { icon: 2 })
                                }
                            }
                        })
                    },
                    //发送打印通知
                    SendSMSForPrint: function() {
                        var that = this;
                        layer.confirm("打印通知只针对未打印的学生发送，是否确认继续发送？", function(index) {
                            lg.request({
                                url: "/api/sys/apply/SendSMSForPrint",
                                method: "post",
                                data: { examId: that.exam.Id },
                                success: res => {
                                    layer.close(index);
                                    if (res.code == 0) {
                                        layer.msg(res.msg, { icon: 1 });
                                    } else {
                                        layer.msg(res.msg, { icon: 2 })
                                    }
                                }
                            })
                        })
                    },
                    //发送成绩查询通知
                    SendSMSForQuery: function() {
                        var that = this;
                        layer.confirm("即将对本次考试的所有考生发送成绩查询通知，是否确认继续发送？", function(index) {
                            lg.request({
                                url: "/api/sys/apply/SendSMSForQuery",
                                method: "post",
                                data: { examId: that.exam.Id },
                                success: res => {
                                    layer.close(index);
                                    if (res.code == 0) {
                                        layer.msg(res.msg, { icon: 1 });
                                    } else {
                                        layer.msg(res.msg, { icon: 2 })
                                    }
                                }
                            })
                        })
                    },
                    //考试安排按钮事件
                    ExamSettingEvent: function() {
                        //判断科目是否存在
                        if (this.exam.Project) {
                            //初始化参数
                            if (this.examSetor.rule.length == 0) {
                                var rule = lg.convert.toJson(this.exam.ExamRule,null);
                                if (rule) {
                                    this.examSetor.rule = rule;
                                } else {
                                    var projects = this.exam.Project.split("|");
                                    if (projects.length > 0) {
                                        for (let i in projects) {
                                            this.examSetor.rule.push({
                                                "km": projects[i],
                                                "sd": []
                                            })
                                        }
                                    }
                                }
                                this.examSetor.currProject = this.examSetor.rule[0];
                            }
                            if (this.exam.SetExamState == 2) {
                                this.examSetor.actTags = ["step-1"]
                            }
                            else {
                                this.examSetor.actTags = ["step-2"]
                            }
                            this.ExamSetorWrapReset();
                            layer.open({
                                type: 1,
                                area: ["800px", "600px"],
                                content: $("#examSettingWrap")
                            })
                        }
                        else {
                            layer.msg("缺少科目，无法继续执行考试按排！", { icon: 2, anim: 6 })
                        }

                    },
                    //查看已按排的考试规则
                    ViewRuleEvent: function() {
                        var rule = JSON.parse(this.exam.ExamRule);
                        if (rule) {
                            this.examSetor.rule = rule;
                            this.examSetor.currProject = this.examSetor.rule[0];
                            this.InitExamWrap("step-3");
                        }
                    },
                    //重新按排考试按钮事件
                    ExamResetEvent: function() {
                        this.examSetor.rule = [];
                        var projects = this.exam.Project.split("|");
                        if (projects.length > 0) {
                            for (let i in projects) {
                                this.examSetor.rule.push({
                                    "km": projects[i],
                                    "sd": []
                                })
                            }
                        }
                        this.examSetor.currProject = this.examSetor.rule[0];
                        this.InitExamWrap("step-2");
                    },
                    //快速按排考试按钮事件
                    ExamQuickSetEvent: function() {
                        this.examSetor.quickData.sd = [];
                        var projects = this.exam.Project.split("|");
                        if (projects.length > 0) {
                            for (let i in projects) {
                                this.examSetor.quickData.sd.push({
                                    "name": projects[i],
                                    "time": ""
                                })
                            }
                        }
                        this.InitExamWrap("step-4");
                    },
                    //快速按排考试
                    QuickSetExam: function() {
                        if (this.examSetor.quickData.seat < 1) {
                            layer.msg("考场容纳人数必须大于0！");
                            return false;
                        }
                        var rules = lg.obj.copy(this.examSetor.rule);
                        var quickdata = this.examSetor.quickData;
                        var rooms = [];
                        var count = Math.ceil((this.exam.StudentCount) / quickdata.seat);
                        for (let i = 1; i <= count; i++) {
                            rooms.push({ "name": quickdata.roomTitle + i, "seat": quickdata.seat })
                        }
                        for (let i in quickdata.sd) {
                            var sd = quickdata.sd[i];
                            var rule = rules.filter(p => p.km == sd.name)[0];
                            rule.sd = [{
                                time: sd.time,
                                number: this.exam.StudentCount,
                                rooms: rooms
                            }]
                        }
                        this.examSetor.rule = rules;
                        this.examSetor.currProject = this.examSetor.rule[0];
                        this.InitExamWrap("step-3");
                    },
                    //提交考试按排规则并执行规则
                    SubmitExamSetData: function() {
                        lg.request({
                            url: "/api/sys/apply/setExam",
                            method: "post",
                            data: { examRule: JSON.stringify(this.examSetor.rule), examId: this.exam.Id },
                            success: res => {
                                if (res.code == 0) {
                                    layer.closeAll();
                                    layer.msg(res.msg, { icon: 1 })
                                    this.GetExam();
                                } else {
                                    layer.msg(res.msg, { icon: 2 })
                                }
                            }
                        })
                    },
                    //考试按排页面科目点击事件
                    ExamProjectEvent: function(item) {
                        this.ExamSetorWrapReset();
                        this.examSetor.currProject = item;
                    },
                    //添加时段按钮事件
                    AddTimeEvent: function() {
                        var number = this.GetCurrProjectNoSetNumber();
                        if (number <= 0) {
                            layer.msg("当前考生已全部按排完毕，无需继续添加时段！", { icon: 2, anim: 6 });
                            return false
                        } else {
                            this.examSetor.sd.number = number;
                            this.examSetor.timeWrapShow = true;
                        }
                    },
                    //添加考试时段
                    AddExamTime: function() {
                        this.examSetor.currProject.sd.push(lg.obj.copy(this.examSetor.sd));
                        this.ExamSetorWrapReset();
                    },
                    //控制活动窗口容器
                    InitExamWrap: function(name, type = 0) {
                        if (type == 0) {
                            this.examSetor.actTags = [name];
                        } else {
                            var arry = this.examSetor.actTags.filter(p => p != name);
                            if (arry.length == this.examSetor.actTags.length) {
                                this.examSetor.actTags.push(name);
                            } else {
                                this.examSetor.actTags = arry;
                            }
                        }
                    },
                    //添加时段弹层全部关闭
                    ExamSetorWrapReset: function() {
                        this.examSetor.timeWrapShow = false;
                        this.examSetor.roomWrapShow = false;
                        this.examSetor.sd = {
                            time: "",
                            number: 0,
                            rooms: [],
                        };
                        this.examSetor.room = { name: "", seat: 0 };
                    },
                    //获取当前科目未按排考试的人数
                    GetCurrProjectNoSetNumber: function() {
                        var sd = this.examSetor.currProject.sd;
                        var readyCount = 0;
                        for (let i in sd) {
                            readyCount += parseInt(sd[i].number)
                            //var rooms = sd[i].rooms;
                            //for (let j in rooms) {
                                
                            //}
                        }
                        return this.exam.StudentCount - readyCount;
                    },
                    OnFormDone: function(item, val) {
                        //console.log(item, val)
                    },
                    //添加考场
                    AddExamRoom: function(n) {
                        if (this.examSetor.room.seat < 1) {
                            layer.msg("考场容纳人数必须大于0！");
                            return false;
                        }
                        var sd_number = this.examSetor.sd.number
                        var sd_rooms = this.examSetor.sd.rooms;
                        var room_seat = this.examSetor.room.seat;
                        var room_name = this.examSetor.room.name
                        var sd_rooms_number = 0;
                        if (sd_number > 0 && room_seat > 0) {
                            for (let i in sd_rooms) {
                                sd_rooms_number += sd_rooms[i].seat
                            }
                            if (sd_rooms_number > sd_number) {
                                layer.msg("当前考场容纳人数已满足该时段的学生数，无需再添加新考场", { icon: 2, anim: 6 });
                                return false;
                            }
                            if (n == 1) {
                                sd_rooms.push({ "name": room_name, "seat": room_seat })
                            }
                            else if (n == 2) {
                                var count = Math.ceil((sd_number - sd_rooms_number) / room_seat);
                                for (let i = 1; i <= count; i++) {
                                    sd_rooms.push({ "name": room_name + count, "seat": room_seat })
                                }
                            }
                            this.examSetor.roomWrapShow = false;
                            this.examSetor.room = { "name": "", seat: 0 }
                        } else {
                            layer.msg("时段总人数和当前考场容纳人数需大于0！", { icon: 2, anim: 6 })
                        }

                    },
                    //学生考场信息按钮事件
                    StudentExamInfoEvent: function(stu) {
                        var that = this;
                        this.currStudent = lg.obj.copy(stu);
                        var json = lg.convert.toJson(this.currStudent.ExamRule, null);
                        if (json == null) {
                            var arry = [];
                            if (this.exam.Project) {
                                var projects = this.exam.Project.split("|");
                                if (projects.length > 0) {
                                    for (let i in projects) {
                                        arry.push({ km: projects[i], time: "", kc: "", seat: "" })
                                    }
                                }
                            }

                            json = arry;
                        }
                        this.currStudent.ExamRule = json;
                        layer.open({
                            type: 1,
                            area: ["600px", "400px"],
                            btn: ["提交修改"],
                            content: $("#stu_exam_set_data"),
                            yes: function(box) {
                                lg.request({
                                    url: "/api/sys/apply/EditExamStudent",
                                    method: "post",
                                    data: { id: that.currStudent.Id, ExamRule: JSON.stringify(that.currStudent.ExamRule), TicketNum: that.currStudent.TicketNum },
                                    success: res => {
                                        layer.close(box);
                                        if (res.code == 0) {
                                            layer.msg("修改成功！", { icon: 1 });
                                            that.GetExamStudent();
                                        } else {
                                            layer.msg(res.msg, { icon: 2 })
                                        }
                                    }
                                })
                            }
                        })
                    },
                    //发送单个打印通知
                    SendSinglePrintSMSEvent: function(stu) {
                        var that = this;
                        layer.confirm("将对学生["+stu.Name+"]发送打印通知,是否确认继续执行？", function(index) {
                            lg.request({
                                url: "/api/sys/apply/SendSMSForPrint",
                                method: "post",
                                data: { examId: that.exam.Id,id:stu.Id },
                                success: res => {
                                    layer.close(index);
                                    if (res.code == 0) {
                                        layer.msg(res.msg, { icon: 1 });
                                    } else {
                                        layer.msg(res.msg, { icon: 2 })
                                    }
                                }
                            })
                        })
                    },
                    //删除学生
                    DeleteExamStudentEvent:function(stu){
                        var that = this;
                        layer.confirm("考生删除后无法恢复！是否确认继续执行？", function(box) {
                            layer.close(box);
                            lg.request({
                                url:"/api/sys/apply/DeleteExamStudent",
                                data: {ids:stu.Id},
                                method:"post",
                                success:res=>{
                                    if (res.code == 0) {
                                        layer.msg("删除成功！", { icon: 1 });
                                        that.GetExamStudent();
                                    }else{
                                        layer.msg(res.msg, {icon:2})
                                    }
                                }
                            })
                        })
                    },
                    //获取学生准考证模板内容
                    GetPrintTemplate: function(stuId) {
                        return new Promise(function(resolve, reject) {
                            lg.request({
                                url: "/api/sys/apply/GetExamStudentTicketTemplate",
                                data: { id: stuId },//考生Id,
                                success: res => {
                                    resolve(res);
                                }
                            })
                        })
                    }
                }
            })
            freeForm.regComponent(app);
            page.vm = app.mount("#lg-ExamInfo");
        }
    }).mount();
</script>
